import { Toast } from 'react-vant';

let initdata = {
    arr:[], // 请求数据的数组
    shoparr:[], // 购物车的数组
    checkall:false ,// 用于控制全选的变量
    addressarr:[
        {
            name:"张三",
            address:"北京市昌平区",
            phone:"11111111111"
        }
    ],
    pullarr:[]
}
let shopreducer = (state=initdata,{type,payload,ispull})=>{
    switch (type) {
        case "getlist" :
            let newaRR = [...state.arr]
            newaRR = payload
            // state.arr = newaRR
            return {
                ...state,
                arr:newaRR
            }
        case "addshop" :
            let newshopaarr = [...state.shoparr]
            let findIndex = state.shoparr.findIndex((item,index)=>{
                return item.id === payload.id
            })
            if (findIndex !== -1) {
                Toast("您已经加入过购物")
            } else {
                newshopaarr.push(payload)
            }
            return {
                ...state,
                shoparr:newshopaarr
            }
        case "addFn":
            let newshopaarr1 = [...state.shoparr]
           let addshopaarr =  newshopaarr1.map((item,index)=>{
                if (item.id  === payload) {
                    item.number ++
                }
                return item
            })
            return {
                ...state,
                shoparr:addshopaarr
            }
        case "delFn":
            let newshopaarr2 = [...state.shoparr]
            let delshopaarr =  newshopaarr2.map((item,index)=>{
                 if (item.id  === payload) {
                     item.number --
                     if (  item.number < 0) {
                        item.number = 0
                        Toast("您不能在减去了")
                     }
                 }
                 return item
             })
             return {
                 ...state,
                 shoparr:delshopaarr
             }
        case "checkAll" :
             state.checkall = !state.checkall
             let checkAll =  state.checkall
            let checkshoparr =  state.shoparr.map((item,index)=>{
                 item.flag = checkAll
                 return item
             })
             return {
                 ...state,
                 checkall: state.checkall,
                 shoparr: checkshoparr
             }
        case 'checkitem' :
            let checkitemarr = [...state.shoparr]
           let checkitemarr1 =  checkitemarr.map((item,index)=>{
                if (item.id === payload) {
                    item.flag = !item.flag
                }
                return item
            })
           let newcheckall =  checkitemarr1.every((item,index)=>{
                return item.flag
            })
            return {
                ...state,
                shoparr:checkitemarr1,
                checkall:newcheckall
            }

        case "addressfn" :
            let newarressarr = [...state.addressarr]
            newarressarr.push(payload)
            return {
                ...state,
                addressarr:newarressarr
            }
            // return {
            //     ...state,

            // }
        case "delfnarress":
            let newarressarr1 = [...state.addressarr]
            newarressarr1.splice(payload,1)
            return {
                ...state,
                addressarr:newarressarr1

            }
        case "editaddressfn" :
            let newarressarr2 = [...state.addressarr]
           
            newarressarr2[payload.editindex] = payload
          
            console.log(newarressarr2,1111);
            return {
                ...state,
                addressarr:newarressarr2
            }
        case "getpulllist":
            console.log(ispull);
             if (!ispull) {  // ispull 的值如果是一个udfined 就进了这个if了
                let newpullrr = [...state.pullarr]
                newpullrr = payload
                return {
                    ...state,
                    pullarr:newpullrr
                }
             }
            else {
                let newpullrr = [...state.pullarr]
                let  newpullrr1 = [...newpullrr,...payload]
                return {
                    ...state,
                    pullarr:newpullrr1
                }
            }
           
        default  :
           return {
               ...state
           }
    }
}
export default shopreducer